﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Data.SqlClient;

namespace Quanlybanhang
{
    public partial class GiabanForm : Office2007Form
    {
        public GiabanForm()
        {
            InitializeComponent();
            khoitao();
            cmbLoaihang_input();
            cmbLoaithung_input();
            if (txtGia.Text == "0")
            {
                btnThem.Enabled = true;
                btnXoa.Enabled = false;
                btnChinhsua.Enabled = false;
            }
            else
            {
                btnThem.Enabled = false;
                btnXoa.Enabled = true;
                btnChinhsua.Enabled = true;
            }
        }

        //Phuong thuc khoi tao cac gia tri ban dau
        public void khoitao()
        {
            //Lay danh sach nhung chu hang ban hang - kieu = 2
            string cmb_cmd = "select tenhang as 'Tên loại hàng', tendonvi as 'Loại thùng hàng', soluongtrung as 'Số trứng', gia as 'Giá bán' from LOAIHANG, DONVI, GIA where gia.id_tenhang = loaihang.id and gia.id_donvi = donvi.id";
            SqlDataAdapter sda = new SqlDataAdapter(cmb_cmd, MainForm.con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridViewX1.DataSource = dt;
        }

        //Dua vao combobox loaihang
        public void cmbLoaihang_input()
        {
            String cmb_cmd = "select distinct tenhang from loaihang";
            SqlDataAdapter sda = new SqlDataAdapter(cmb_cmd, MainForm.con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            cmbLoaihang.DataSource = dt;
            cmbLoaihang.DisplayMember = "tenhang";
        }

        //Dua vao combobox loaithung
        public void cmbLoaithung_input()
        {
            String cmb_cmd = "select distinct tendonvi from donvi";
            SqlDataAdapter sda = new SqlDataAdapter(cmb_cmd, MainForm.con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            cmbLoaithung.DataSource = dt;
            cmbLoaithung.DisplayMember = "tendonvi";
        }

        public int get_LoaihangID(String str)
        {
            int id = 0;
            String cmd = "select id from Loaihang where tenhang = '" + cmbLoaihang.Text + "'";
            SqlCommand sqlcmd = new SqlCommand(cmd, MainForm.con);
            SqlDataReader rdr = sqlcmd.ExecuteReader();
            while (rdr.Read())
            {
                id = (int)(rdr["id"]);
            }
            rdr.Close();
            return id;
        }

        public int get_LoaithungID(String str)
        {
            int id = 0;
            String cmd = "select id from donvi where tendonvi = '" + cmbLoaithung.Text + "'";
            SqlCommand sqlcmd = new SqlCommand(cmd, MainForm.con);
            SqlDataReader rdr = sqlcmd.ExecuteReader();
            while (rdr.Read())
            {
                id = (int)(rdr["id"]);
            }
            rdr.Close();
            return id;
        }

        public void Gia_setText()
        {
            int gia = 0;
            String cmd = "select gia from Gia where id_tenhang = " + get_LoaihangID(cmbLoaihang.Text)+ " and id_donvi = " + get_LoaithungID(cmbLoaithung.Text)+"";
            SqlCommand sqlcmd = new SqlCommand(cmd, MainForm.con);
            SqlDataReader rdr = sqlcmd.ExecuteReader();
            while (rdr.Read())
            {
                gia = (int)(rdr["gia"]);
            }
            rdr.Close();
            txtGia.Text = gia.ToString();
        }

        private void cmbLoaithung_SelectedIndexChanged(object sender, EventArgs e)
        {
            Gia_setText();
            if (txtGia.Text == "0")
            {
                btnThem.Enabled = true;
                btnChinhsua.Enabled = false;
                btnXoa.Enabled = false;
            }
            else
            {
                btnThem.Enabled = false;
                btnChinhsua.Enabled = true;
                btnXoa.Enabled = true;
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            int sogia = 0;
            string gia = "";
            try
            {
                gia = txtGia.Text;
                sogia = Convert.ToInt32(gia);
                if (sogia < 0)
                    MessageBox.Show("Hãy nhập đúng giá bán");
                else
                {
                    try
                    {
                        SqlCommand sqlComm = new SqlCommand();
                        sqlComm = MainForm.con.CreateCommand();
                        sqlComm.CommandText = @"INSERT INTO GIA VALUES('" + get_LoaihangID(cmbLoaihang.Text) + "', '" + get_LoaithungID(cmbLoaithung.Text) + "', " + sogia + ")";
                        sqlComm.ExecuteNonQuery();
                        MessageBox.Show("Thêm mới thành công!", "Thành công", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        khoitao();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Thêm mới không thành công!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            catch (Exception ee) { MessageBox.Show("Hãy nhập đúng giá bán"); };     
        }

        private void cmbLoaihang_SelectedIndexChanged(object sender, EventArgs e)
        {
            Gia_setText();
            if (txtGia.Text == "0")
            {
                btnThem.Enabled = true;
                btnChinhsua.Enabled = false;
                btnXoa.Enabled = false;
            }
            else
            {
                btnThem.Enabled = false;
                btnChinhsua.Enabled = true;
                btnXoa.Enabled = true;
            }
        }

        private void btnChinhsua_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCommand sqlComm = new SqlCommand();
                sqlComm = MainForm.con.CreateCommand();
                sqlComm.CommandText = @"UPDATE GIA SET gia ='" + txtGia.Text + "' WHERE id_tenhang='" + get_LoaihangID(cmbLoaihang.Text) + "' and id_donvi ='" + get_LoaithungID(cmbLoaithung.Text) + "'";
                sqlComm.ExecuteNonQuery();
                khoitao();
                MessageBox.Show("Lưu lại thành công!", "Thành công", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ee)
            {
                MessageBox.Show("Lưu lại không thành công!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult xoa = MessageBox.Show("Bạn có chắc chắn xóa loại giá này?", "Xóa giá mặt hàng", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (xoa == DialogResult.Yes)
            {
                try
                {
                    SqlCommand sqlComm = new SqlCommand();
                    sqlComm = MainForm.con.CreateCommand();
                    sqlComm.CommandText = @"DELETE GIA WHERE id_tenhang='" + get_LoaihangID(cmbLoaihang.Text) + "' and id_donvi ='" + get_LoaithungID(cmbLoaithung.Text) + "'";
                    sqlComm.ExecuteNonQuery();
                    khoitao();
                    MessageBox.Show("Xóa thành công!", "Thành công", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ee)
                {
                    MessageBox.Show("Xóa không thành công!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

    }
}
